package net.kleinhenz.norabase.client;

import java.util.ArrayList;
import java.util.List;

import net.kleinhenz.norabase.shared.model.Ability;
import net.kleinhenz.norabase.shared.model.Champion;
import net.kleinhenz.norabase.shared.model.Equipment;
import net.kleinhenz.norabase.shared.model.Relic;
import net.kleinhenz.norabase.shared.model.Rune;
import net.kleinhenz.norabase.shared.model.Spell;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class BackCard extends AbsolutePanel {

	private Image cardImg = null;
	private Image maskImg = null;
	private Image borderImg = null;	
	private Label nameLabel = null;
	private Label noraLabel = null;
	private HTML raceLabel = null;
	private HTML classLabel = null;
	private HTML desc = null;
	private VerticalPanel abilities = null;
	private VerticalPanel abilitiesUp = null;
	private List<Label> abilitiesLabels = new ArrayList<Label>();
	private List<Label> abilitiesUpLabels = new ArrayList<Label>();
	
	public BackCard() {

		createLayout();
	}
	
	private void createLayout() {
		
		setSize("310px", "390px");
		
		cardImg = new Image();
		cardImg.getElement().getStyle().setProperty("zIndex", "100");
		add(cardImg, 0, 0);
				
		maskImg = new Image("/icons/mask.png");
		maskImg.getElement().getStyle().setProperty("zIndex", "200");
		add(maskImg, 0, 0);

		borderImg = new Image();
		borderImg.getElement().getStyle().setProperty("zIndex", "300");
		add(borderImg, 0, 0);
		
		nameLabel = new Label();
		nameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		nameLabel.setStyleName("cardTitle");
		nameLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(nameLabel, 0, 13);
		nameLabel.setSize("312px", "23px");
		
		noraLabel = new Label();
		noraLabel.setStyleName("noraCost");
		noraLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(noraLabel, 264, 11);

		raceLabel = new HTML();
		raceLabel.setStyleName("abilities");
		raceLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(raceLabel, 25, 175);

		classLabel = new HTML();
		classLabel.setStyleName("abilities");
		classLabel.getElement().getStyle().setProperty("zIndex", "500");
		add(classLabel, 205, 175);

		abilities = new VerticalPanel();
		abilities.setWidth("220px");
		abilities.getElement().getStyle().setProperty("zIndex", "500");
		add(abilities, 45, 190);

		abilitiesUp = new VerticalPanel();
		abilitiesUp.setWidth("220px");
		abilitiesUp.getElement().getStyle().setProperty("zIndex", "500");
		add(abilitiesUp, 45, 335);

		desc = new HTML();
		desc.setWidth("200px");
		desc.setStyleName("description");
		desc.getElement().getStyle().setProperty("zIndex", "600");
		add(desc, 55, 140);
	}

	private Label createAbilityLabel(Ability ability) {
		
		Label abilitiyLabel = new Label(ability.getName(), true);
		abilitiyLabel.setWidth("100%");
		abilitiyLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		abilitiyLabel.setStyleName("abilities");
		abilitiyLabel.getElement().getStyle().setProperty("zIndex", "500");
		abilitiyLabel.addMouseOverHandler(
			    new TooltipListener(
			      "your texdfsdfsdft", 50 /* timeout in milliseconds*/,"yourcssclass"));

		return abilitiyLabel;
	}
	
	public void loadRune(Rune rune) {
		
		setSize("310px", "390px");

		if (rune instanceof Champion) {
			cardImg.setUrl(PoxNoraURL.back(PoxNoraURL.FACTION.get(rune.getFaction()), 0));
			raceLabel.setHTML("Race: <span style='color:rgb(195, 219, 101);'>" + ((Champion)rune).getRaces() + "</span>");
			classLabel.setHTML("Class: <span style='color:rgb(195, 219, 101);'>" + ((Champion)rune).getClasses() + "</span>");
			
			for (Label aL : abilitiesLabels)
				abilities.remove(aL);
			for (Ability a : ((Champion)rune).getStartingAbilities()) {
				Label aL = createAbilityLabel(a);
				abilitiesLabels.add(aL);
				abilities.add(aL);							
			}

			for (Label aL : abilitiesUpLabels)
				abilitiesUp.remove(aL);
			for (Ability a : ((Champion)rune).getUpgradeAbilities()) {
				Label aL = createAbilityLabel(a);
				abilitiesUpLabels.add(aL);
				abilitiesUp.add(aL);											
			}			
			nameLabel.setText("Champion Details");
			desc.setVisible(false);
		}
		else {
			cardImg.setUrl(PoxNoraURL.back());
			raceLabel.setHTML("");
			classLabel.setHTML("");
			for (Label aL : abilitiesLabels)
				abilities.remove(aL);
			for (Label aL : abilitiesUpLabels)
				abilitiesUp.remove(aL);			
			nameLabel.setText(rune.getName());
			desc.setVisible(true);
			if (rune instanceof Spell)
				desc.setHTML(((Spell)rune).getDesc());
			else if (rune instanceof Equipment)
				desc.setHTML(((Equipment)rune).getDesc());
			else if (rune instanceof Relic)
				desc.setHTML(((Relic)rune).getDesc());
		}
		
		borderImg.setUrl(PoxNoraURL.border(PoxNoraURL.FACTION.get(rune.getFaction()), 0));
		
		noraLabel.setText("" + rune.getNora());
	}
}
